<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
		:close-on-press-escape='false' :fullscreen='true'>
		<el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
			label-width="120px" :inline="true">

			<el-form-item label="盘点号" prop="checkkey">
				<el-input v-model="dataForm.checkkey" placeholder="盘点号" disabled></el-input>
			</el-form-item>
			<!--     <el-form-item label="行号" prop="line">
            <el-input v-model="dataForm.line" placeholder="行号"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-input v-model="dataForm.status" placeholder="状态"></el-input>
          </el-form-item> -->
			<el-form-item label="物料编码" prop="sku">
				<el-input v-model="dataForm.sku" placeholder="物料编码"  @click="openSkuDialogHandle()" ></el-input>
			</el-form-item>
			<el-form-item label="物料名称" prop="descr">
				<el-input v-model="dataForm.descr" placeholder="物料名称"></el-input>
			</el-form-item>
			<el-form-item label="库位编码" prop="location">
				<el-input v-model="dataForm.location" placeholder="库位编码"></el-input>
			</el-form-item>
			<el-form-item label="箱" prop="tray">
				<el-input v-model="dataForm.tray" placeholder="目的箱" disabled></el-input>
				
			</el-form-item>
			<el-form-item label="格" prop="box">
				<!-- <el-input v-model="dataForm.box" placeholder="目的容器"></el-input> -->
				<el-select v-model="dataForm.box" placeholder="请选择">
					<el-option v-for="item in boxs" :key="item.list" :label="item.listname" :value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="数量" prop="qty">
				<el-input v-model="dataForm.qty" placeholder="数量" disabled></el-input>
			</el-form-item>
			<el-form-item label="初盘数量" prop="firstqty">
				<el-input v-model="dataForm.firstqty" placeholder="初盘数量"></el-input>
			</el-form-item>
			<el-form-item label="复盘数量" prop="secondqty">
				<el-input v-model="dataForm.secondqty" placeholder="复盘数量"></el-input>
			</el-form-item>
			<!-- <el-form-item label="初盘人" prop="firstuser">
				<el-input v-model="dataForm.firstuser" placeholder="初盘人"></el-input>
			</el-form-item>
			<el-form-item label="初盘时间" prop="firsttime">
				<el-input v-model="dataForm.firsttime" placeholder="初盘时间"></el-input>
			</el-form-item>
			<el-form-item label="复盘人" prop="seconduser">
				<el-input v-model="dataForm.seconduser" placeholder="复盘人"></el-input>
			</el-form-item>
			<el-form-item label="复盘时间" prop="secondaddtime">
				<el-input v-model="dataForm.secondaddtime" placeholder="复盘时间"></el-input>
			</el-form-item> -->
			<el-form-item label="批号" prop="lot">
				<el-input v-model="dataForm.lot" placeholder="批号"></el-input>
			</el-form-item>
			<el-form-item label="生产日期" prop="lot1">
				<el-input v-model="dataForm.lot1" placeholder="生产日期"></el-input>
			</el-form-item>
			<el-form-item label="有效日期" prop="lot2">
				<el-input v-model="dataForm.lot2" placeholder="有效日期"></el-input>
			</el-form-item>
			<el-form-item label="是否加长" prop="lot3">
				<el-input v-model="dataForm.lot3" placeholder="是否加长"></el-input>
			</el-form-item>
			<el-form-item label="价格" prop="lot4">
				<el-input v-model="dataForm.lot4" placeholder="价格"></el-input>
			</el-form-item>
			<el-form-item label="部门/供应商" prop="lot5">
				<el-input v-model="dataForm.lot5" placeholder="部门/供应商"></el-input>
			</el-form-item>
			<el-form-item label="计划单号/工序号" prop="lot6">
				<el-input v-model="dataForm.lot6" placeholder="计划单号/工序号"></el-input>
			</el-form-item>
			<el-form-item label="主制车间" prop="lot7">
				<el-input v-model="dataForm.lot7" placeholder="主制车间"></el-input>
			</el-form-item>
			<el-form-item label="任务编码" prop="lot8">
				<el-input v-model="dataForm.lot8" placeholder="任务编码"></el-input>
			</el-form-item>
			<el-form-item label="新旧" prop="lot9">
				<el-input v-model="dataForm.lot9" placeholder="新旧"></el-input>
			</el-form-item>
			<el-form-item label="作废" prop="lot10">
				<el-input v-model="dataForm.lot10" placeholder="作废"></el-input>
			</el-form-item>
			<el-form-item label="炉批号" prop="lot11">
				<el-input v-model="dataForm.lot11" placeholder="炉批号"></el-input>
			</el-form-item>
			<el-form-item label="材料复验号" prop="lot12">
				<el-input v-model="dataForm.lot12" placeholder="材料复验号"></el-input>
			</el-form-item>
			<el-form-item label="牌号" prop="lot13">
				<el-input v-model="dataForm.lot13" placeholder="牌号"></el-input>
			</el-form-item>
			<el-form-item label="属性14" prop="lot14">
			  <el-input v-model="dataForm.lot14" placeholder="属性14"></el-input>
			</el-form-item>
			<el-form-item label="属性15" prop="lot15">
			  <el-input v-model="dataForm.lot15" placeholder="属性15"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>
			<!-- <el-form-item label="回传状态" prop="backstatus">
				<el-input v-model="dataForm.backstatus" placeholder="回传状态"></el-input>
			</el-form-item>
			<el-form-item label="回传时间" prop="backdate">
				<el-input v-model="dataForm.backdate" placeholder="回传时间"></el-input>
			</el-form-item>
			<el-form-item label="回传结果" prop="backresult">
				<el-input v-model="dataForm.backresult" placeholder="回传结果"></el-input>
			</el-form-item>
			<el-form-item label="回传码" prop="backcode">
				<el-input v-model="dataForm.backcode" placeholder="回传码"></el-input>
			</el-form-item>
			<el-form-item label="仓库" prop="warehouse">
				<el-input v-model="dataForm.warehouse" placeholder="仓库"></el-input>
			</el-form-item>
			<el-form-item label="创建人" prop="adduser">
				<el-input v-model="dataForm.adduser" placeholder="创建人"></el-input>
			</el-form-item>
			<el-form-item label="创建时间" prop="addtime">
				<el-input v-model="dataForm.addtime" placeholder="创建时间"></el-input>
			</el-form-item>
			<el-form-item label="修改人" prop="updatuser">
				<el-input v-model="dataForm.updatuser" placeholder="修改人"></el-input>
			</el-form-item>
			<el-form-item label="修改时间" prop="updatetime">
				<el-input v-model="dataForm.updatetime" placeholder="修改时间"></el-input>
			</el-form-item> -->
		</el-form>
		<div style="margin-top: 40px;font-size: 18px" v-show="isuniqueVisible">
			<span>唯一码管理:</span>
		</div>
		<div style="margin-top: 15px">
			<el-form :inline="true" v-show="isuniqueVisible">
				<el-form-item label="物料编码" prop="sku">
					<el-input v-bind:model-value="dataForm.sku" placeholder="物料编码" disabled></el-input>
				</el-form-item>
				<el-form-item label="物料名称" prop="descr">
					<el-input v-bind:model-value="dataForm.descr" placeholder="物料名称" disabled></el-input>
				</el-form-item>
				<el-form-item label="唯一码" prop="uniqueCode">
					<el-input v-model="message" placeholder="唯一码" @change="putMessage"></el-input>
				</el-form-item>
				<el-form-item label="已输入的唯一码">
					<el-input type="textarea" :autosize="{ minRows: 8, maxRows: 20}" placeholder="已输入的唯一码"
						style="width: 500px" v-model="dataForm.uniquecodes">
					</el-input>
				</el-form-item>
			</el-form>
		</div>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="saveCheckdetailInFn()">确定</el-button>
		</template>
	</el-dialog>
	<SkuDialog v-if="skuDialogVisible" ref="skuDialogVue" @fillline="fillLine"></SkuDialog>
</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		getCheckdetailById,
		putCheckdetail,
		postCheckdetail,
		getcheckdetailKeyAndLocationByTray,
		saveCheckdetailIn
	} from "@/network/lswms/check/Checkdetail"
	import {
		ElMessage
	} from 'element-plus'
	import _ from 'lodash'

	import {getCodeListByCode} from "@/network/lswms/base/Codelist.js"
	import SkuDialog from "@/components/lswms/base/SkuDialog.vue"

	export default {
		name: "CheckdetailTrayInAddOrUpdate",
		emits: ["refreshDataList"],
		components: {
			SkuDialog
		} ,
		setup(props, {
			emit
		}) {
			const visible = ref(false);
			const dataForm = ref({
				id: '',
				checkkey: '',
				line: '',
				status: '',
				sku: '',
				descr: '',
				lot: '',
				location: '',
				tray: '',
				box: 'B01',
				qty: 0,
				firstqty: 0,
				firstuser: '',
				firsttime: '',
				secondqty: 0,
				seconduser: '',
				secondaddtime: '',
				lot1: '',
				lot2: '',
				lot3: '',
				lot4: '',
				lot5: '',
				lot6: '',
				lot7: '',
				lot8: '',
				lot9: '',
				lot10: '',
				lot11: '',
				lot12: '',
				lot13: '',
				lot14: '',
				lot15: '',
				remark: '',
				backstatus: '',
				backdate: '',
				backresult: '',
				backcode: '',
				warehouse: '',
				adduser: '',
				addtime: '',
				updatuser: '',
				updatetime: '',
				isuniquecode: '',
				uniquecodes: ''
			});


			const dataRule = reactive({
				checkkey: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				/*  line: [
				    { required: true, message: '必填项不能为空', trigger: 'blur' }
				  ],          status: [
				    { required: true, message: '必填项不能为空', trigger: 'blur' }
				  ],     */
				sku: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				/*  lot: [
				    { required: true, message: '必填项不能为空', trigger: 'blur' }
				  ],     */
				location: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				tray: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				box: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				qty: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				firstqty: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				/*  firstuser: [
				    { required: true, message: '必填项不能为空', trigger: 'blur' }
				  ],          firsttime: [
				    { required: true, message: '必填项不能为空', trigger: 'blur' }
				  ],      */
				secondqty: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				/*      seconduser: [
				        { required: true, message: '必填项不能为空', trigger: 'blur' }
				      ],          secondaddtime: [
				        { required: true, message: '必填项不能为空', trigger: 'blur' }
				      ],   */
				/* lot1: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          lot2: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          lot3: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          lot4: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          lot5: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          lot6: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          lot7: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          lot8: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          lot9: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          lot10: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          remark: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          backstatus: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          backdate: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          backresult: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          backcode: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          warehouse: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          adduser: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          addtime: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          updatuser: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ],          updatetime: [
				      { required: true, message: '必填项不能为空', trigger: 'blur' }
				    ]  */
			})

			const dataFormRef = ref(null)

			const init = () => {
				visible.value = true;
				nextTick(function() {
					dataFormRef.value.resetFields();
	
					getcheckdetailKeyAndLocation();
					

				})
				boxsFn();
			}

			const getcheckdetailKeyAndLocation = () => {
				getcheckdetailKeyAndLocationByTray(dataForm.value.tray).then(function(res) {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value = _.assignIn({}, dataForm.value, res.data);
				}).catch((err) => {});
			}
			
			const skuDialogVisible = ref(false);
			const skuDialogVue = ref(null);
			const openSkuDialogHandle = () => {
				skuDialogVisible.value = true;
				nextTick(function() {
					//skuDialogVue.value.dataForm.sku = dataForm.value.sku;
					skuDialogVue.value.init();
				})
			
			}
			
			const fillLine = (SkuRow)=>{
				dataForm.value.sku = SkuRow.sku;
				dataForm.value.descr = SkuRow.descr;
				dataForm.value.isuniquecode= SkuRow.isuniquecode;
			}
			
			
			const boxs = ref("");
			
			const boxsFn = () => {
			
				getCodeListByCode('BOXLIST').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					boxs.value = res.data;
				}).catch((err) => {});
			}
			
			const isuniqueVisible = computed(()=>{
				return dataForm.value.isuniquecode==1;
			});
			const message = ref('')
			const putMessage = () => {
				if (message.value == '') {
					return;
				}
				let uniquecodes = dataForm.value.uniquecodes.split('\n')
				let newArr = uniquecodes.filter(function(value, index, array) {
					return value == message.value;
				});
			
				if (newArr.length == 0) {
					if (dataForm.value.uniquecodes.length == 0) {
						dataForm.value.uniquecodes = message.value
					} else {
						dataForm.value.uniquecodes = dataForm.value.uniquecodes + '\n' + message.value
					}
				}
				message.value = ''
			}

			// 表单提交
			const saveCheckdetailInFn = _.debounce(() => {
				dataFormRef.value.validate(function(valid) {
					if (!valid) {
						return false;
					}

					
					 saveCheckdetailIn(dataForm.value).then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								visible.value = false
								emit('refreshDataList')
							}
						})
					}).catch(function() {});

				})
			}, 1000, {
				'leading': true,
				'trailing': false
			})

			return {
				visible,
				dataForm,
				dataRule,
				init,
				saveCheckdetailInFn,
				dataFormRef,
				openSkuDialogHandle,
				skuDialogVisible,
				skuDialogVue,
				fillLine,
				boxs,
				putMessage,
				message,
				isuniqueVisible
			}
		},


	}
</script>

<style scoped>

</style>
